package com.jason.redCherry.action;

import com.jason.redCherry.common.Global;
import com.jason.redCherry.common.StatusCodeManager;
import com.jason.redCherry.common.ToolUtil;
import com.jason.redCherry.dao.ConfigMapper;
import com.jason.redCherry.dao.GuestMapper;
import com.jason.redCherry.dao.UserMapper;
import com.jason.redCherry.entity.Guest;
import com.jason.redCherry.entity.User;
import com.jason.redCherry.entity.response.GuestResp;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Jason on 2016/9/5.
 */
@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class LoginAction extends BasicAction {

    private String userName;
    private String pwd;

    private String mac;
    private String ip;


    private String appVersion;

    @Resource
    public GuestMapper guestMapper;
    @Resource
    ConfigMapper configMapper;
    @Resource
    private UserMapper userMapper;

    //登陆校验
    public void check() {
        //1.校验用户密码
        Guest guest = guestMapper.selectByPrimaryKey(userName);
        if (guest == null) {
            resultMsg(StatusCodeManager.ERROR_NO_USER);
            return;
        }

        //账户不可用
        if (guest.getStatus() == 1) {
            resultMsg(StatusCodeManager.ERROR_CANT_USE);
            return;
        }
        //更新用户的mac地址
        guest.setMacAddress(mac);
        guest.setIpAddress(ip);
        guest.setLastTime(new Date());
        guestMapper.updateByPrimaryKey(guest);

        if (!guest.getPwd().equals(pwd)) {
            resultMsg(StatusCodeManager.ERROR_PWD);
            return;
        }

        //2.校验使用期限
        Date now = new Date();
        if (guest.getUntilTime() != null && now.compareTo(guest.getUntilTime()) > 0) {
            resultMsg(StatusCodeManager.ERROR_TIMT_UNTIL);
            return;
        }
        //3.校验版本信息
        /*Config config = configMapper.selectByKey(Global.CONFIG_APP_VERSION);
        if (appVersion == null) {
            appVersion = "0.0.1";
        }
        if (config == null || !config.getSysVal().equals(appVersion)) {
            resultMsg(StatusCodeManager.ERROR_APP_VERSION);
            return;
        }*/
        resultMsg(StatusCodeManager.SUCCESS, convert(guest));
    }


    private GuestResp convert(Guest guest) {

        GuestResp guestResp = new GuestResp();
        //guestResp.setIpAddress(guest.getIpAddress());
        //guestResp.setMacAddress(guest.getIpAddress());
        //guestResp.setCreateTime(ToolUtil.getDateStr(guest.getCreateTime()));
        guestResp.setGuestId(guest.getGuestId());
        //guestResp.setLastTime(ToolUtil.getDateStr(guest.getLastTime()));
        guestResp.setOwner(guest.getOwner());
        guestResp.setPwd(guest.getPwd());
        guestResp.setUntilTime(ToolUtil.getDateStr(guest.getUntilTime()));

        return guestResp;
    }


    public String user() {
        User user = userMapper.selectByUserName(userName);
        if (user == null) {
            setAttr("errorInfo", "用户不存在");
            return "login";
        }
        if (!user.getPwd().equals(pwd)) {
            setAttr("errorInfo", "密码错误");
            return "login";
        }

        setSession(Global.SESSION_CURRENT_USER, user);

        if (user.getUserRealName().equals("0")) {
            return "rule_redirect";
        } else {
            return "guest_redirect";
        }
    }


    public String logout() {
        clearCurrentUser();
        return "login";
    }


    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public GuestMapper getGuestMapper() {
        return guestMapper;
    }

    public void setGuestMapper(GuestMapper guestMapper) {
        this.guestMapper = guestMapper;
    }

    public ConfigMapper getConfigMapper() {
        return configMapper;
    }

    public void setConfigMapper(ConfigMapper configMapper) {
        this.configMapper = configMapper;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }
}
